import { http } from '@/utils/request';

export interface Department {
  id: string;
  name: string;
  code: string;
  description?: string;
  parentId?: string;
  level: number;
  sort: number;
  status: number;
  managerId?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface CreateDepartmentRequest {
  name: string;
  code: string;
  description?: string;
  parentId?: string;
  level?: number;
  sort?: number;
  managerId?: string;
}

export interface UpdateDepartmentRequest {
  id: string;
  name?: string;
  code?: string;
  description?: string;
  parentId?: string;
  level?: number;
  sort?: number;
  status?: number;
  managerId?: string;
}

export interface DepartmentWithChildren extends Department {
  children?: DepartmentWithChildren[];
  users?: any[];
}

// 获取部门树
export async function fetchDepartmentTree() {
  const response = await http.get<DepartmentWithChildren[]>('/system/department/tree');
  return response;
}

// 获取部门列表
export async function fetchDepartmentList() {
  const response = await http.get<Department[]>('/system/department/list');
  return response;
}

// 创建部门
export async function createDepartment(data: CreateDepartmentRequest) {
  const response = await http.post<Department>('/system/department', data);
  return response;
}

// 更新部门
export async function updateDepartment(data: UpdateDepartmentRequest) {
  const response = await http.put<Department>('/system/department', data);
  return response;
}

// 删除部门
export async function deleteDepartment(id: string) {
  const response = await http.delete<void>(`/system/department/${id}`);
  return response;
}

// 获取部门下的用户列表
export async function fetchUsersByDepartment(departmentId: string) {
  const response = await http.get<any[]>(`/system/department/${departmentId}/users`);
  return response;
}
